package com.xgk.boot.module.core.controller.admin.customer;

import com.xgk.boot.framework.common.pojo.CommonResult;
import com.xgk.boot.framework.common.pojo.PageResult;
import com.xgk.boot.framework.common.util.object.BeanUtils;
import com.xgk.boot.module.core.controller.admin.customer.vo.*;
import com.xgk.boot.module.core.dal.entity.customer.CustomerAddressDO;
import com.xgk.boot.module.core.dal.entity.customer.CustomerDO;
import com.xgk.boot.module.core.service.customer.CustomerAddressServiceImpl;
import com.xgk.boot.module.core.service.customer.CustomerServiceImpl;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "客户发货地址管理")
@RestController("customerAddressController")
@RequestMapping("/system/customer/address")
public class CustomerAddressController {

    @Resource
    private CustomerAddressServiceImpl customerAddressService;

    @PostMapping({"/save"})
    @Operation(summary = "保存发货地址")
    public CommonResult<CustomerAddressVO> saveAddress(@Valid  @RequestBody CustomerAddressSaveVO addressVO) {
        CustomerAddressDO custAddress = customerAddressService.createCustAddress(addressVO);
        return CommonResult.success(BeanUtils.toBean(custAddress,CustomerAddressVO.class));
    }

    @PutMapping({"/update/{id}"})
    @Operation(summary = "更新发货地址")
    public CommonResult<CustomerAddressVO> updateAddress(@PathVariable("id") Long id,@Valid  @RequestBody CustomerAddressSaveVO addressVO) {
        CustomerAddressDO customerAddressDO = customerAddressService.updateCustAddress(id, addressVO);
        return CommonResult.success(BeanUtils.toBean(customerAddressDO,CustomerAddressVO.class));
    }


    @GetMapping("/get/{id}")
    @Operation(summary = "获得发货地址详情")
    @Parameter(name = "id", description = "发货地址id", required = true, example = "1934923143484133377")
    public CommonResult<CustomerAddressVO> getCustomerAddress(@PathVariable("id") Long id) {
        CustomerAddressDO customerAddressDO = customerAddressService.selectByIdFromCache(id);
        return  CommonResult.success(BeanUtils.toBean(customerAddressDO,CustomerAddressVO.class));
    }

//    @GetMapping("/get_by_code")
//    @Operation(summary = "通过code获得发货地址详情")
//    @Parameter(name = "code", description = "发货地址code", required = true, example = "C0001")
//    public CommonResult<CustomerAddressVO> getCustAddressByCode(@RequestParam("code") String code) {
//        return  CommonResult.success(null);
//    }


    @PostMapping("/page")
    @Operation(summary = "分页获得当前用户发货地址列表")
    public CommonResult<PageResult<CustomerAddressVO>> pageListCustAddress(@Valid @RequestBody CustomerAddressReqVO addressReqVO) {
        PageResult<CustomerAddressDO> customerAddressDOPageResult = customerAddressService.pageCustomerAddress(addressReqVO);
        return CommonResult.success(BeanUtils.toBean(customerAddressDOPageResult,CustomerAddressVO.class));
    }

//    @PostMapping("/list")
//    @Operation(summary = "获得当前用户权限的发货地址列表")
//    public CommonResult<List<CustomerListVO>> listAddress(@Valid @RequestBody CustomerAddressReqVO addressReqVO) {
//        return CommonResult.success(null);
//    }


    @DeleteMapping("/delete/{id}")
    @Operation(summary = "删除发货地址")
    @Parameter(name = "id", description = "发货地址id", required = true, example = "1934923143484133377")
    public CommonResult<Boolean> deleteCustomerAddress(@PathVariable("id") Long id) {
        return  CommonResult.success(customerAddressService.deleteAddress(id));
    }

}
